#!/bin/bash
# Author: fanyongfeng
# 59 23 * * * /bin/bash /root/script/mongo_log_backup.sh &>> /backup/mongo_log_backup/backup.log

TODAY=`date +%Y%m%d`
BACKUP_DIR=/backup/mongo_log_backup
LOG_FILE_PATH=/data/mongodb-4.0.28/cluster/mongos/logs/mongod.log

mkdir -p ${BACKUP_DIR}
\cp -p ${LOG_FILE_PATH} ${BACKUP_DIR}/mongod.log.${TODAY}
[[ $? -eq 0 ]] && echo "${TODAY} mongo log backup success" || echo "${TODAY} mongo log backup failed"
cd ${BACKUP_DIR}
gzip mongod.log.${TODAY} && truncate -s 0 ${LOG_FILE_PATH}

find ${BACKUP_DIR} -type f -mtime +180 -name "mongod.log.*.gz" | xargs -r -n 10 rm -f